study.loadFriends = function() {
    var req = opensocial.newDataRequest();
    var viewerFriendsIdSpec = opensocial.newIdSpec({'userId': 'VIEWER', 'groupId': 'FRIENDS'});
    var opt_params = {};
    opt_params[opensocial.DataRequest.PeopleRequestFields.MAX] = 100;
    req.add(req.newFetchPeopleRequest(viewerFriendsIdSpec, opt_params), 'viewerFriends');
    req.send(study.onLoadFriends);
};

study.onLoadFriends = function(data) {
    var viewerFriends = data.get('viewerFriends').getData();
    var html = [];
    html.push('<select id="person">');
    viewerFriends.each(function(person) {
	    html.push('<option value="' + person.getId() + '">' + person.getDisplayName() + '</option>');
	}
    );
};

study.saveViewerData = function() {
    var req = opensocial.newDataRequest();
    var data1 = Math.random() * 5;
    var data2 = Math.random() * 100;
    var data3 = new Date().getTime();
    req.add(req.newUpdatePersonAppDataRequest('VIEWER', 'AppField1', data1));
    req.add(req.newUpdatePersonAppDataRequest('VIEWER', 'AppField2', data2));
    req.add(req.newUpdatePersonAppDataRequest('VIEWER', 'AppField3', data3));
};

study.requestMyData = function() {
    var req = opensocial.newDataRequest();
    var fields = ['AppField1', 'AppField2', 'AppField3'];
    var p = {};
    p[opensocial.IdSpec.Field.USER_ID[]] = 'VIEWER';
    var idSpec = opensocial.newIdSpec(p);
    req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER), 'viewer');
    req.add(req.newFetchPersonAppDataRequest(idSpec, fields), 'viewer_data');
    req.send(study.handleRequestMyData);
};

study.handleRequestMyData = function(data) {
    var mydata = data.get('viewer_data');
    var viewer = data.get('viewer');
    me = viewer.getData();
    if (mydata.hadError()) {
	htmlout += data.getErrorMessage();
	return;
    }
};

study.eraseSingleField = function() {
    var req = opensocial.newDataRequest();
    req.add(req.newRemovePersonAppDataRequest('VIEWER','myKey'), 'clear_data');
    req.send(some_callback);
};

study.eraseMultipleFields = function() {
    var req = opensocial.newDataRequest();
    req.add(req.newRemovePersonAppDataRequest('VIEWER', ['key1','key2','key3']), 'clear_data');
    req.send(some_callback);
};

